package com.hxd.mall.module.product

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import com.hxd.mall.R
import com.hxd.mall.module.product.search.bean.ProductDetailBean
import com.hxd.mall.utils.local.NavigationUtil.jumpToWebView
import com.hxd.mall.utils.open.StringUtil.setIconText
import com.hxd.mall.utils.open.ViewUtils.isHidden
import com.hxd.mall.utils.open.addSetOnClickListener
import kotlinx.android.synthetic.main.product_details_top_layout.view.*
import android.view.animation.Animation
import android.view.animation.AnimationSet
import com.hxd.mall.utils.open.load
import android.text.SpannableStringBuilder
import android.text.Spannable
import android.text.SpannableString
import android.text.style.BackgroundColorSpan
import android.widget.TextView
import com.hxd.mall.utils.open.StringUtil
import android.view.animation.LinearInterpolator
import java.lang.Exception
import android.view.animation.AnimationUtils
import com.hxd.mall.utils.local.*
import org.jetbrains.anko.displayMetrics


/**
 * Create by yinzhengwei on 2019-11-11
 * @Function
 */
class ProductTopView(context: Context) : LinearLayout(context, null, 0) {
    //是否隐藏图片详情
    private var isHide = true

    init {
        LayoutInflater.from(context).inflate(R.layout.product_details_top_layout, this)
    }

    @SuppressLint("SetTextI18n")
    fun reLoad(mBean: ProductDetailBean?) {
        addSetOnClickListener(rules, get_coup, layout_details_tip) {
            onclick(it, mBean)
        }

        val layoutParams = picScan.layoutParams
        layoutParams.height = picScan.context.displayMetrics.widthPixels
        picScan.layoutParams = layoutParams

        picScan.setImages(mBean?.data?.pictUrl ?: ArrayList())
        picScan.show()
        mBean?.data?.run {
            price.text = discountPrice
            originPrice.text = if ("tmall" == subChannel) {
                "天猫价:$reservePrice"
            } else {
                "淘宝价:$reservePrice"
            }
            originPrice?.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
            tv_bug_get.text = "购买后可得${estimatedIncome}元现金"
            person_bug.text = "${volume}人已付款"
            shop_store_name.text = shopInfo?.shopName
            shop_store_icon.load(shopInfo?.icon)
            shop_desc.text = "宝贝描述  ${shopInfo.descScore}"
            getString(shopInfo.descScore?.toDouble()?:0.0, shopInfo.dsrPercent?.toDouble()?:0.0, shop_desc_end, shop_desc)
            shop_service.text = "卖家服务  ${shopInfo.serviceScore}"
            getString(shopInfo.serviceScore?.toDouble()?:0.0, shopInfo.servicePercent?.toDouble()?:0.0, shop_service_end, shop_service)
            ship_score.text = "物流服务  ${shopInfo.shipScore}"
            getString(shopInfo.shipScore?.toDouble()?:0.0, shopInfo.shipPercent?.toDouble()?:0.0, ship_score_end, ship_score)
//            setTextBg()
            if (!isHidden(couponAmount, coup_layout)) {
                coupMoney.text = couponAmount
            }

            //productTitle.setIconAndText(if ("tmall" == subChannel) R.mipmap.tm_icon else R.mipmap.tb_icon, title)

            productTitle.setTextIsSelectable(false)
            productTitle.setOnLongClickListener {
                setPrimaryClip(context, title)

                //为了防止在别的界面出现弹窗，将显示的状态重置
                PrimaryUtil.saveBooleanData(title, true)
                false
            }
            setIconText(productTitle, iv_logo_icon, subChannel, title)
        }
    }

    private fun onclick(it: View, mBean: ProductDetailBean?) {
        when (it) {
            get_coup -> {
                it.context?.sendPoint(commidity_coupon_get_button_click)
                (context as ProductDetailActivity).buyProduct()
            }
            rules -> {
                it.context?.sendPoint(cash_back_rule)
                val url = mBean?.data?.ruleUri ?: ""
                jumpToWebView(it.resources.getString(R.string.return_ruler), url)
            }
            layout_details_tip -> {
                rotateAnim(isHide)
                (context as ProductDetailActivity).fetchProductImageList(isHide)
                isHide = !isHide
            }
        }
    }

    fun getString(num1: Double, num2: Double, tv: TextView, tv2: TextView) {
        tv.run {
            when {
                num1 > num2 -> {
                    text = "高"
                    setTextColor(resources.getColor(R.color.color_E44C43))
                    setBackgroundResource(R.drawable.shape_circle_product_bg)
                    tv2.text = SpannableString(tv2.text.toString().trim()).apply {
                        StringUtil.setSpanClick(this, 6, tv2.text.toString().trim().length) {
                        }
                    }
                }
                num1 == num2 -> {
                    text = "平"
                    setTextColor(resources.getColor(R.color.color_FF999999))
                    setBackgroundResource(0)
                }
                else -> {
                    text = "低"
                    setTextColor(resources.getColor(R.color.color_FF999999))
                    setBackgroundResource(0)
                }
            }
        }
    }

    fun rotateAnim(flag: Boolean) {
        var objectAnimator = ObjectAnimator.ofFloat(iv_show_detail, "rotation", 90f, 0f)
        objectAnimator.duration = 700
        objectAnimator.repeatCount = 0
        objectAnimator.repeatMode = ValueAnimator.REVERSE
        if (flag) {
            objectAnimator = ObjectAnimator.ofFloat(iv_show_detail, "rotation", 0f, 90f)
        }
        objectAnimator.start()
    }

}